package com.example.photoshit;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;


public class MainActivity extends Activity {
    //用来显示单个图像
    private ImageView imageOne;

    //用来显示所有的图像
    private ImageView imageAll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageOne = (ImageView) findViewById(R.id.imageView);
//新建一个Gallery类，这是是实现拖动效果的关键
        //Gallery is 一个用来水平卷动显示对象的视图
        Gallery gallery = (Gallery) findViewById(R.id.gallery);
        gallery.setAdapter(new ImageAdapter(this));
    }

    //实现ImageResource接口获得自己创建的图像资源
    class ImageAdapter extends BaseAdapter implements ImageResource {
        //每一个gallery中图像的背景资源
        private int galleryItemBackground;
        private Context context;


        public ImageAdapter(Context context) {
            this.context = context;
            //这里实现的功能上就是上半部分每个图像那个背景框
            // 对应的xml文件就是attr.xml
            TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
            galleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
            a.recycle();


        }

        @Override
        public int getCount() {

            return dImageID.length;
        }


        @Override
        public Object getItem(int position) {

            return position;
        }


        @Override
        public long getItemId(int position) {
//将此索引的图像设为imageOne显示
            imageOne.setImageResource(ImageAdapter.dImageID[position]);
            imageOne.setScaleType(ImageView.ScaleType.FIT_CENTER);
            return position;
        }

        //这个方法返回的ImageView就是实现拖动效果的图像
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            imageAll = new ImageView(context);
//设置图像源
            imageAll.setImageResource(dImageID[position]);
//设置视图为120*120
            imageAll.setLayoutParams(new Gallery.LayoutParams(125, 125));
//设置图像相对于视图的比例，FIT_XY表示充满X和Y轴
            imageAll.setScaleType(ImageView.ScaleType.FIT_XY);
            //设置imageAll中每一个Item的背景资源
            imageAll.setBackgroundResource(galleryItemBackground);
            return imageAll;
        }

    }
}